<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\card\model;

class CardCount extends \app\BaseModel
{
    protected $name = "longbing_card_count";
    protected static function init()
    {
    }
    public function getYesterdaylist($where)
    {
        $data = $this->where($where)->whereDay("create_time", "yesterday")->field("to_uid as user_id,type,sign,count(to_uid) as number,uniacid")->group("to_uid")->order("create_time", "desc")->select()->toArray();
        if($data) {
            $stat = new \app\dynamic\model\CardStatistics();
            foreach ($data as $key => $val) {
                $data[$key]["create_time"] = strtotime("-1 day");
                $data[$key]["table"] = "CardCount";
            }
            $stat->createRows($data);
        }
        return $data;
    }
    public function getYesterday($where)
    {
        return $this->where($where)->whereDay("create_time", "yesterday")->count();
    }
    public function gettoday($where)
    {
        return $this->where($where)->whereDay("create_time")->count();
    }
    public function zxInfo($user_id, $where = [], $type = 0)
    {
        if($type == 1) {
            $de = "=";
        } else {
            $de = "in";
        }
        $where1 = [["to_uid", $de, $user_id], ["sign", "=", "copy"], ["type", "=", "8"], $where];
        $where2 = [["to_uid", $de, $user_id], ["sign", "=", "praise"], ["type", "=", "5"], $where];
        $count = $this->where([array_filter($where1)])->whereor([array_filter($where2)])->count();
        return $count;
    }
    public function getCount($where5)
    {
        $data = $this->where($where5)->count();
        return $data;
    }
    public function radarList($where = [], $page = 1, $list_rows = 20)
    {
        $data = self::where($where)->alias("a")->field(["a.*", "b.nickName as name"])->join("longbing_card_user b", "a.user_id = b.id")->join("longbing_card_collection c", "a.user_id = c.uid && a.to_uid = c.to_uid")->order(["a.id" => "desc"])->paginate(["list_rows" => $list_rows, "page" => $page])->toArray();
        if(!empty($data["data"])) {
            $data["data"] = lbHandelRadarMsg($data["data"]);
        }
        return $data;
    }
    public function doPageBossInterest($where, $uniacid)
    {
        $filter = [["sign", "=", "copy"], ["uniacid", "=", $uniacid], $where, ["type", "in", [6, 7, 11]]];
        $filter1 = [["sign", "=", "view"], ["type", "=", 6], ["uniacid", "=", $uniacid], $where];
        $compony = $this->where([$filter])->whereor([$filter1])->count();
        $filter2 = [["sign", "=", "copy"], ["uniacid", "=", $uniacid], $where, ["type", "=", 8]];
        $where3 = [["sign", "=", "view"], ["uniacid", "=", $uniacid], $where, ["type", "in", [1, 2, 11, 15, 16, 17, 19, 20, 25, 26, 27, 28, 29, 30]]];
        $where21 = [["sign", "=", "praise"], ["uniacid", "=", $uniacid], $where, ["type", "in", [5, 6, 7, 8]]];
        $where31 = [["sign", "=", "order"], ["uniacid", "=", $uniacid], $where];
        $goods = $this->where([$filter2])->whereor([$where3])->whereor([$where21])->whereor([$where31])->count();
        $where4 = [["sign", "=", "copy"], ["uniacid", "=", $uniacid], $where, ["type", "in", [1, 2, 3, 4, 5, 9, 10]]];
        $where5 = [["sign", "=", "praise"], ["uniacid", "=", $uniacid], $where, ["type", "in", [1, 2, 3, 4]]];
        $staff = $this->where([$where4])->whereor([array_filter($where5)])->count();
        $total = $compony + $goods + $staff;
        $data = [["data" => $compony, "rate" => 0, "name" => "对公司感兴趣"], ["data" => $goods, "rate" => 0, "name" => "对产品感兴趣"], ["data" => $staff, "rate" => 0, "name" => "对我感兴趣"]];
        if($total) {
            foreach ($data as $k => $v) {
                $data[$k]["rate"] = sprintf("%.2f", $v["data"] / $total) * 100;
            }
        }
        return $data;
    }
    public function active($staff_id, $uniacid, $type = 0)
    {
        if($type == 1) {
            $str = "in";
        } else {
            $str = "=";
        }
        $data = [];
        $lastday = date("Y/m/d");
        $number = self::where([["to_uid", $str, $staff_id], ["create_time", "between", [strtotime($lastday . " 00:00:00"), strtotime($lastday . " 23:59:59")]]])->count();
        $data[] = ["date" => date("m/d"), "number" => $number];
        $max = $number;
        for ($i = 1; $i < 15; $i++) {
            $start_time = strtotime($lastday . " -" . $i . " days");
            $md = date("m/d", $start_time);
            $ymd = date("Y/m/d", $start_time);
            $end_time = strtotime($ymd . " 23:59:59");
            $number = self::where([["to_uid", $str, $staff_id], ["create_time", "between", [$start_time, $end_time]]])->count();
            if($max < $number) {
                $max = $number;
            }
            $data[] = ["date" => $md, "number" => $number];
        }
        return ["data" => $data, "max" => $max];
    }
    public function getsss($staff_id, $uniacid, $type = 0)
    {
        if($type == 1) {
            $str = "=";
        } else {
            $str = "in";
        }
        $data = ["max_number" => 0, "total" => 0, "data" => []];
        $count = self::where([["sign", "=", "copy"], ["to_uid", $str, $staff_id], ["type", "=", 8], ["uniacid", "=", $uniacid]])->count();
        $data["max_number"] = $count;
        $data["total"] += $count;
        $data["data"][] = ["name" => "咨询产品", "number" => $count];
        $count = self::where([["sign", "=", "copy"], ["to_uid", $str, $staff_id], ["type", "=", 1], ["uniacid", "=", $uniacid]])->count();
        if($data["max_number"] < $count) {
            $data["max_number"] = $count;
        }
        $data["total"] += $count;
        $data["data"][] = ["name" => "保存电话", "number" => $count];
        $count = self::where([["sign", "=", "copy"], ["to_uid", $str, $staff_id], ["uniacid", "=", $uniacid], ["type", "in", [2, 3, 11]]])->count();
        $count += self::where([["sign", "=", "praise"], ["to_uid", $str, $staff_id], ["uniacid", "=", $uniacid], ["type", "=", 7]])->count();
        if($data["max_number"] < $count) {
            $data["max_number"] = $count;
        }
        $data["total"] += $count;
        $data["data"][] = ["name" => "拨打电话", "number" => $count];
        $view_base_filter = [["to_uid", $str, $staff_id], ["sign", "=", "view"], ["uniacid", "=", $uniacid]];
        $view_shop_filter = $view_base_filter;
        $view_shop_filter[] = ["type", "=", 1];
        $view_shop_number = self::where($view_shop_filter)->count();
        $data["total"] += $view_shop_number;
        $data["data"][] = ["name" => "浏览商城", "number" => $view_shop_number];
        $view_goods_filter = $view_base_filter;
        $view_goods_filter[] = ["type", "=", 2];
        $view_goods_number = self::where($view_goods_filter)->count();
        $data["total"] += $view_goods_number;
        $data["data"][] = ["name" => "浏览商品", "number" => $view_goods_number];
        $view_official_web_filter = $view_base_filter;
        $view_official_web_filter[] = ["type", "=", 6];
        $view_official_web_number = self::where($view_official_web_filter)->count();
        $data["total"] += $view_official_web_number;
        $name = tabbarName(4, $uniacid);
        $data["data"][] = ["name" => !empty($name) ? "查看" . $name : "查看官网", "number" => $view_official_web_number];
        $view_timeline_filter = $view_base_filter;
        $view_timeline_filter[] = ["type", "=", 3];
        $view_timeline_number = self::where($view_timeline_filter)->count();
        $data["total"] += $view_timeline_number;
        $data["data"][] = ["name" => "浏览动态", "number" => $view_timeline_number];
        $praise_filter = [["to_uid", $str, $staff_id], ["uniacid", "=", $uniacid], ["sign", "=", "praise"], ["type", "IN", [1, 3]]];
        $praise_number = self::where($praise_filter)->count();
        $data["total"] += $praise_number;
        $data["data"][] = ["name" => "点赞", "number" => $praise_number];
        $share_filter = [["to_uid", $str, $staff_id], ["uniacid", "=", $uniacid], ["sign", "=", "praise"], ["type", "=", 4]];
        $share_number = self::where($share_filter)->count();
        $data["total"] += $share_number;
        $data["data"][] = ["name" => "分享", "number" => $share_number];
        $sort_arr = [];
        foreach ($data["data"] as $k => $v) {
            $sort_arr[] = $v["number"];
        }
        array_multisort($sort_arr, SORT_DESC, $data["data"]);
        $data["max_number"] = $data["data"][0]["number"] ?? 0;
        foreach ($data["data"] as $index => $item) {
            if($data["max_number"] == 0) {
                $rate = 0;
            } else {
                $rate = $item["number"] / $data["max_number"] * 100;
            }
            $rate = sprintf("%.0f", $rate);
            $data["data"][$index]["rate"] = $rate;
        }
        return $data;
    }
    public function getCountUser($where, $time)
    {
        $data = $this->alias("a")->join("longbing_card_user_info b", "a.to_uid = b.fans_id")->where($where)->whereDay("a.create_time", $time)->group("user_id")->count();
        return $data;
    }
}

?>